package com.cicigodd.brick.plug.pay.controller;

import com.cicigodd.brick.plug.common.response.Result;
import com.cicigodd.brick.plug.common.response.ResultGenerator;
import com.cicigodd.brick.plug.annotation.resource.ApiResource;
import com.cicigodd.brick.plug.annotation.resource.GetResource;
import com.cicigodd.brick.plug.annotation.resource.PostResource;
import com.cicigodd.brick.plug.pay.service.WeixinPayService;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author cicigodd
 * @date 2022-10-11 10:06
 */
@Api(tags = "微信支付")
@RestController
public class WxPayController {

    @Resource
    private WeixinPayService weixinPayService;

    /**
     * 统一下单
     * @param params
     * @return
     */
    @ApiOperation(value = "统一下单", notes = "统一下单")
    @PostResource(name = "统一下单", path = "/wxPay/unifiedOrder")
    public Result unifiedOrder(@RequestBody Map<String,String> params) {
        String outTradeNo = params.get("outTradeNo");
        String code = params.get("code");
        Map<String,Object> wxPayUnifiedOrderResult = weixinPayService.unifiedOrder(outTradeNo,code);
        return ResultGenerator.successResult(wxPayUnifiedOrderResult);
    }

    /**
     * 付款微信回调
     *
     * @param body 微信返回参数
     * @return
     */
    @ApiIgnore
    @ApiOperation(value = "付款回调", notes = "付款回调")
    @ApiResource(name = "付款回调", path = "/wxPay/notify/pay")
    public Object payNotify(@RequestBody String body) {
        return weixinPayService.payNotify(body);
    }

    /**
     * 查询订单
     * @param outTradeNo
     * @return
     */
    @ApiOperation(value = "查询订单", notes = "查询订单")
    @GetResource(name = "查询订单", path = "/wxPay/queryOrder")
    public Result queryOrder(String outTradeNo) {
        WxPayOrderQueryResult wxPayOrderQueryResult = weixinPayService.queryOrder(outTradeNo);
        return ResultGenerator.successResult(wxPayOrderQueryResult);
    }

    /**
     * 申请退款
     * @param id
     * @return
     */
    @ApiOperation(value = "申请退款", notes = "申请退款")
    @PostResource(name = "申请退款", path = "/wxPay/refund")
    public Result refund(String id) {
        weixinPayService.refund(id);
        return ResultGenerator.successResult();
    }

    /**
     * 退款回调
     *
     * @throws WxPayException
     */
    @ApiIgnore
    @ApiOperation(value = "退款回调", notes = "退款回调")
    @ApiResource(name = "退款回调", path = "/wxPay/notify/refund")
    public Object refundNotify(@RequestBody String body) {
        return weixinPayService.refundNotify(body);
    }
}
